import { getUser, listUser } from '@/api/modules/user/user';
import { allowEditMenu, query, table } from '../data/data';

import { listRole } from '@/api/modules/auth/roles';
import { resetForm, userInfo } from './form';

export const create = async () => {
  allowEditMenu.value = true;

  resetForm();
  const { data } = await listRole();
  userInfo.value.roles = data.data;
};
export const list = async () => {
  table.value.loading = true;
  const { data } = await listUser(query.value);
  table.value.loading = false;
  table.value.data = data.rows;
  query.value.total = data.total;
};

export const edit = async (id?: string) => {
  const targetId = id ? id : table.value.keys[0];
  const { data } = await getUser(`${targetId}`);
  userInfo.value = data.data;

  allowEditMenu.value = true;
  table.value.loading = false;
};

export const remove = async (id?: string) => {
  const ids = id ? [id] : table.value.keys;
  await list();
};

export const detail = () => {
  allowEditMenu.value = true;
  table.value.loading = false;
};
export const search = () => {
  query.value.pageNum = 1;
  list();
};
